package dao;

import model.Blog;
import util.DBUtils;

import java.nio.FloatBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 对博客表相关的操作
 */
public class BlogDao {
    /**
     * 获取所有的博客列表
     * @return
     */
    public List<Blog> selectAll(){
        List<Blog> blogs = new ArrayList<>();
        Connection connection = DBUtils.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from blog where delete_flag=0";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            //可能返回多条
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setId(resultSet.getInt("id"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if (content.length()>50){
                    content = content.substring(0,50)+"...";
                }
                content = content.replace("#","");
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("user_id"));
                blog.setDeleteFlag(resultSet.getInt("delete_flag"));
                blog.setCreateTime(resultSet.getTimestamp("create_time"));
//                blog.setUpdateTime(resultSet.getTimestamp("update_time"));
                blogs.add(blog);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.close(connection,statement,resultSet);
        }
        return blogs;
    }
    /**
     * 根据博客ID,获取博客的信息
     */
    public Blog selectById(Integer blogId){
        Blog blog = null;
        Connection connection = DBUtils.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from blog where delete_flag=0 and id =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            //可能返回多条
            if (resultSet.next()){
                blog = new Blog();
                blog.setId(resultSet.getInt("id"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("user_id"));
                blog.setDeleteFlag(resultSet.getInt("delete_flag"));
                blog.setCreateTime(resultSet.getTimestamp("create_time"));
//                blog.setUpdateTime(resultSet.getTimestamp("update_time"));

            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.close(connection,statement,resultSet);
        }
        return blog;
    }
    /**
     * 插入博客信息
     */
    public void insertBlog(Blog blog){
        Connection connection = DBUtils.getConnection();
        PreparedStatement statement = null;
        try {
            String sql = "insert into blog (`title`,`content`,`user_id`) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());
            statement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.close(connection,statement,null);
        }
    }
    /**
     * 删除博客信息
     */
    public void deleteBlog(Integer blogId){
        Connection connection = DBUtils.getConnection();
        PreparedStatement statement = null;
        try {
            String sql = "update blog set delete_flag=1 where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.close(connection,statement,null);
        }
    }
}